package com.ruoyi.project.hcy.product.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.hcy.product.IllegalCountException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.product.domain.series.ProSeries;
import com.ruoyi.project.hcy.product.mapper.ProSeriesMapper;
import com.ruoyi.project.hcy.product.mapper.ProductMapper;
import com.ruoyi.project.hcy.product.service.IProSeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ProSeriesServiceImpl
 * @Author SAI
 * @Date 2020/9/17 14:53
 * @Description 产品系列业务实现类
 * @Version 1.0
 */
@Service("series")
public class ProSeriesServiceImpl implements IProSeriesService {

    @Autowired
    private ProSeriesMapper proSeriesMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询产品系列集合
     *
     * @param proSeries
     * @return
     */
    @Override
    public List<ProSeries> selectSeriesList(ProSeries proSeries) {
        return proSeriesMapper.selectSeriesList(proSeries);
    }

    /**
     * 检查系列名字是否唯一
     *
     * @param proSeries
     * @return
     */
    @Override
    public String checkseriesNameUnique(ProSeries proSeries) {

        Long seriesId = StringUtils.isNull(proSeries.getSeriesId()) ? -1L : proSeries.getSeriesId();
        ProSeries info = proSeriesMapper.checkseriesNameUnique(proSeries.getSeriesName());
        if (StringUtils.isNotNull(info) && info.getSeriesId().longValue() != seriesId.longValue()) {
            return UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }

    /**
     * 添加产品系列
     * @param proSeries
     * @return
     */
    @Override
    public int insertProSeries(ProSeries proSeries) {
        return proSeriesMapper.insertProSeries(proSeries);
    }

    /**
     * 根据id查询单个产品系列
     * @param seriesId
     * @return
     */
    @Override
    public ProSeries selectSeriesById(Long seriesId) {
        return proSeriesMapper.selectSeriesById(seriesId);
    }

    /**
     * 根据提供的对象修改产品系列，实际调用的是根据id修改
     * @param proSeries
     * @return
     */
    @Override
    public int updateProSeries(ProSeries proSeries) {
        return proSeriesMapper.updateProSeries(proSeries);
    }

    /**
     * 前端传来id数组，但以string形式传输，需要转换
     * 主要识别是否有产品已经占用了，如果占用了，那么不能删除
     * @param ids
     * @return
     */
    @Override
    public int deleteProSeriesByIds(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        for (Long id : longIds) {
//        这里应该是调用根据该SeriesId查询是否有产品，有的话不能删除
            int i = productMapper.countSeriesProductById(id);
            System.out.println(i);
            if (productMapper.countSeriesProductById(id) > 0) {
                ProSeries proSeries = proSeriesMapper.selectSeriesById(id);
                throw new IllegalCountException(String.format("%1$s已被产品使用,不能删除", proSeries.getSeriesName()));
            }
        }
        return proSeriesMapper.deleteProSeriesByIds(longIds);
    }
}
